<template>
	<view v-if="data">
		<view style="display: flex;flex-direction: column;height: 100vh;justify-content: space-between;">
			<view style="flex: 1;overflow: scroll;background-color: #eee;">
				<!-- <u-cell-group>
				<u-cell-item  :title="'核发店面'" ></u-cell-item>
				<u-cell-item icon="map" :title="title" @click="show=true"></u-cell-item>
			</u-cell-group> -->
				<u-form v-if="curCus.bmn == '1' || curCus.bsjmn == '1'" class="form bg-white" :model="model" ref="uForm"
					label-position="left">
					<u-form-item label="核发店面:" prop="testCarVen" label-width="200"
						:label-style="{'font-weight':'bold'}">
						<js-select :childName="'None'" v-model="model.code" :showFilter="true" :items="selectList"
							placeholder="请选择核发店面" :tree="false" :label-value="model.name"
							@label-input="model.name = $event">
						</js-select>
						<!-- @confirm="selectConfirm" -->
					</u-form-item>
				</u-form>

				<view style="margin-top: 10px;background-color: #fff;padding: 10px;">
					<u-section title="商品清单" :right="false" line-color="#00aa00" font-size="32"></u-section>

					<view style="padding: 10px;border-top: 1px solid #eee ;margin-top: 10px;">
						<view v-for="(item,index) in list" :key="index">
							<view v-if="item.busType == '0'"
								style="display: flex;margin: 10px 0;border-bottom: 1px solid #eee; ">
								<image class="item-menu-image" mode="aspectFit"
									:src="item.shopInv.basInv.picUrl?vuex_config.baseUrl + item.shopInv.basInv.picUrl:''">
								</image>
								<view style="margin-left: 10px; flex: 1;">
									<view style="font-weight: bold;font-size: 16px;margin-bottom: 10px;">
										{{item.shopInv.basInv.cinvname}}
									</view>
									<view>
										<view style="color: #aaa;">
											规格：{{item.shopInv.basInv.cinvstd?item.shopInv.basInv.cinvstd:''}}</view>
										<!-- <view style="background:  bisque ;padding: 5px;color: #ff874b;border-radius: 5px">1件起订</view> -->
									</view>
									<view style="display: flex;justify-content: space-between;margin-top: 10px;">
										<view>
											<text
												style="color: red;font-size: 20px;">￥{{item.shopInv.basInv.appPrice?item.shopInv.basInv.appPrice:'暂无单价' }}</text>
											<text>/{{item.shopInv.basInv.appUnitName}}</text>
										</view>
										<view>x {{item.iqty}}</view>
									</view>
								</view>
							</view>
							<view v-if="item.busType == '1'"
								style="display: flex;margin: 10px 0;border-bottom: 1px solid #eee; ">
								<image class="item-menu-image" mode="aspectFit"
									:src="item.shopProductSell.picUrl?vuex_config.baseUrl + item.shopProductSell.picUrl:''">
								</image>
								<view style="margin-left: 10px; flex: 1;">
									<view style="font-weight: bold;font-size: 16px;margin-bottom: 10px;">
										{{item.shopProductSell.abbName}}
									</view>
									<view>
										<view style="color: #aaa;">
											简介：{{item.shopProductSell.ctitle?item.shopProductSell.ctitle:''}}</view>
										<!-- <view style="background:  bisque ;padding: 5px;color: #ff874b;border-radius: 5px">1件起订</view> -->
									</view>
									<view style="display: flex;justify-content: space-between;margin-top: 10px;">
										<view>
											<text
												style="color: red;font-size: 20px;">￥{{item.shopProductSell.fprice?item.shopProductSell.fprice:'暂无单价' }}</text>
											<!-- <text>/{{item.shopProductSell.basInv.appUnitName}}</text> -->
											<text>/{{item.shopProductSell.unitName}}</text>
										</view>
										<view>x {{item.iqty}}</view>
									</view>
								</view>
							</view>

						</view>
						<!-- <view style="display: flex;margin: 10px 0;">
						<image class="item-menu-image" :src="'https://cdn.uviewui.com/uview/common/classify/1/2.jpg'" mode=""></image>
						<view style="margin-left: 10px;">
							<view style="font-weight: bold;font-size: 16px;margin-bottom: 10px;">蒙牛悠瑞富硒高钙羊奶粉800gX6罐蒙牛悠牛悠</view>
							<view >
								<view style="color: #aaa;">规格:800g*6</view>
							</view>
							<view style="display: flex;justify-content: space-between;margin-top: 10px;">
								<view>
									<text style="color: red;font-size: 20px;">￥990.00</text>
									<text>/件</text>
								</view>
								<view>x 2</view>
							</view>
						</view>
					</view> -->


					</view>
				</view>

				<view style="margin-top: 10px;background-color: #fff;padding: 10px;">
					<view style="display: flex;justify-content: space-between; margin-bottom: 5px;">
						<text>优惠金额</text>
						<text>￥{{data.disMoney}}</text>
					</view>
					<view style="display: flex;justify-content: space-between; margin-bottom: 5px;">
						<text>商品总价</text>
						<text style="color: red;">￥{{data.sumMoney}}</text>
					</view>
					<view style="display: flex;justify-content: space-between; margin-bottom: 5px;">
						<text>经销商余额</text>
						<text
							:style="{color:data.cusMoney.appZhMoney<0?'red':'#000'}">￥{{data.cusMoney.appZhMoney}}</text>
					</view>
					<view style="display: flex;justify-content: space-between; margin-bottom: 5px;">
						<text>经销商返利余额</text>
						<text>￥{{data.cusMoney.appFlMoney}}</text>
					</view>
					<view style="display: flex;justify-content: space-between; margin-bottom: 5px;">
						<text>经销商促销余额</text>
						<text>￥{{data.cusMoney.appCxMoney}}</text>
					</view>
					<view style="display: flex;justify-content: space-between; margin-bottom: 5px;">
						<text>信用额度</text>
						<text>￥{{data.cusMoney.xyMoney}}</text>
					</view>
				</view>

				<view style="margin: 10px 0;background-color: #fff;padding: 10px;">
					<view style="display: flex;justify-content: space-between;">
						<u-section title="返利使用" :right="false" line-color="#00aa00" font-size="32"></u-section>
						<view style="color: red;text-align: right;">
							<view class="">
								促销返利金额上限: {{data.sumMoneyLmit}}
							</view>
							<!-- <view class="">
							Tips:返利金额上限: {{data.flMoneyLmit}}
						</view>
						<view class="">
							促销金额上限: {{data.cxMoneyLmit}}
						</view> -->
						</view>

					</view>

					<view style="padding: 10px;border-top: 1px solid #eee ;margin-top: 10px;margin-right: 10px;">
						<view style="display: flex;justify-content: space-between;">
							<view>经销商返利余额</view>
							<view>￥{{cusMoney.flhxMoney}}</view>
							<view><u-icon name="edit-pen" :size="50" @click="modify(1)"></u-icon></view>
						</view>
						<view style="display: flex;justify-content: space-between; margin: 20px 0;">
							<view>经销商促销余额</view>
							<view>￥{{cusMoney.cxhxMoney}}</view>
							<view><u-icon name="edit-pen" :size="50" @click="modify(2)"></u-icon></view>
						</view>
						<view>
							备注
							<!-- <u-input v-model="textarea" placeholder="" type="textarea" :auto-height="flase" height="50" maxlength="256" /> -->
							<textarea v-model="remarks" type="text" placeholder-class="line" placeholder="请输入备注信息" />
						</view>
					</view>
				</view>




			</view>

			<view
				style="padding: 20px;display: flex;justify-content: space-between;border-top: 1px solid #eee;background-color: #fff;">
				<view>
					合计:
					<text style="color: red;font-size: 20px;">
						<!-- ￥{{payMoney}} -->
						<!-- ￥{{data.sumMoney}} -->
						￥{{data.payMoney}}
					</text>
				</view>
				<text @click="submit"
					style="padding: 10px 25px;background: #00aa00;color: #fff;border-radius: 5px">提交订单</text>
				<!--  @click="submit" -->
			</view>


			<u-modal v-model="numShow" :mask-close-able="true" :show-cancel-button="true" :show-title="false"
				@confirm="confirm">
				<view style="text-align: center;padding: 20px 30px 10px;">
					<view style="margin-bottom: 10px;">
						{{flag == '1'?`返利金额修改`:`促销金额修改`}}
					</view>
					<!-- <u-number-box v-model="value" :input-width="200" :positive-integer="false"
					:nodes="content"></u-number-box> -->
					<u-input v-model="value" type="digit" placeholder="0.00" :focus="focus" :border="true"
						@input="replaceInput" clearable />
					<view style="color: red;text-align: left;font-size: 14px;margin-top: 10px;">
						<text> {{flag == '1'?`返利金额上限：`:`促销金额上限：`}}</text>
						<text style="font-size: 16px;">￥{{flag == '1'?data.flMoneyLmit:data.cxMoneyLmit}}</text>
					</view>
				</view>
			</u-modal>

		</view>
	</view>
</template>

<script>
	import util from '@/common/fire.js'
	export default {

		data() {
			return {
				focus: false,
				remarks: '',
				value: 0,
				list: [],
				payMoney: 0,
				show: false,
				numShow: false,
				title: "选择收货地址",
				curCus: {},
				data: '',
				cusMoney: {
					flhxMoney: '0.00',
					cxhxMoney: '0.00'
				},
				flag: '',
				model: {},
				selectList: []
			}
		},
		onLoad(params) {

			let that = this
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('acceptData', function(data) {
				that.data = data


				that.data.cusMoney.xyMoney = util.toFixed2(data.cusMoney.xyMoney)

				that.data.disMoney = util.toFixed2(data.disMoney)
				that.data.sumMoney = util.toFixed2(data.sumMoney)

				that.data.flMoneyLmit = util.toFixed2(data.flMoneyLmit)
				that.data.cxMoneyLmit = util.toFixed2(data.cxMoneyLmit)

				that.data.payMoney = util.toFixed2(data.payMoney)
				// that.data.zhMoney=util.toFixed2(data.zhMoney)
				// that.data.flMoney=util.toFixed2(data.flMoney)
				// that.data.cxMoney=util.toFixed2(data.cxMoney)
				// that.cusMoney = {...that.data.cusMoney}
				// that.cusMoney.flhxMoney=util.toFixed2(that.cusMoney.flhxMoney)
				// that.cusMoney.cxhxMoney=util.toFixed2(that.cusMoney.cxhxMoney)


				that.data.sumMoneyLmit = util.toFixed2(data.sumMoneyLmit)


				that.data.cusMoney.appZhMoney = util.toFixed2(data.cusMoney.appZhMoney)
				that.data.cusMoney.appFlMoney = util.toFixed2(data.cusMoney.appFlMoney)
				that.data.cusMoney.appCxMoney = util.toFixed2(data.cusMoney.appCxMoney)

				that.list = that.data.carList.map(item => {
					if (item.busType == '0') {
						item.shopInv.basInv.appPrice = util.toFixed2(item.shopInv.basInv.appPrice)
					} else {
						item.shopProductSell.fprice = util.toFixed2(item.shopProductSell.fprice)
					}
					return item
				})
				that.payMoney = util.toFixed2(that.data.payMoney)

			})
			// console.log(params,'params=======');
			// this.data = JSON.parse(params.list)
			// this.cusMoney = this.data.cusMoney
			// this.list = this.data.carList
			// this.payMoney = this.data.payMoney

			this.$u.api.shop.findMnCusListData().then(res1 => {
				this.selectList = res1.map(item => {
					item.value = item.code
					item.name = item.cusDefine1?`${item.name}/ ${item.cusDefine1}`:item.name
					return item
				})
			});
			this.$u.api.shop.getCurCus()
				.then(res => {
					this.curCus = {
						bmn: res.bmn,
						bsjmn: res.bsjmn
					}
				});


		},
		methods: {
			replaceInput(e) {
				console.log(e);
				var that = this
				e = e.match(/^\d*(\.?\d{0,2})/g)[0]
				this.$nextTick(() => {
					that.value = e
				})

			},
			submit() {
				// curCus.bmn == '1' || curCus.bsjmn == '1'
				console.log(this.model.code)
				if ((this.curCus.bmn == '1' && !this.model.code) || (this.curCus.bsjmn == '1' && !this.model.code)) {
					this.$u.toast('请选择核发门店');
				} else {
					let params = {
						hfmdCode: this.model.code ? this.model.code : '',
						cusCode: this.vuex_basCus.code,
						cusMoney: this.data.cusMoney,
						disMoney: this.data.disMoney,
						// zhMoney:this.data.zhMoney,
						// flMoney:this.data.flMoney,
						// cxMoney:this.data.cxMoney,
						// zhMoney:this.data.zhMoney,
						zhMoney: this.payMoney,
						flMoney: this.cusMoney.flhxMoney,
						cxMoney: this.cusMoney.cxhxMoney,
						sumMoney: this.data.sumMoney,
						payMoney: this.payMoney,
						selIds: this.data.carList.map(item => item.id).join(','),
						carList: this.data.carList,
						remarks: this.remarks

					}
					console.log(params);
					this.$u.api.shop.saveOrderFromShopCar(params)
						.then(res => {
							if (res.result == 'true') {
								this.$u.toast(res.message);
								setTimeout(() => {
									uni.navigateBack({
										delta: 1,
									})
								}, 500)

							} else {
								uni.showModal({
									title: '提示',
									content: res.message,
									showCancel: false,
									success: function(res) {}
								});
							}

						})
				}

			},
			// address(){
			// 	uni.navigateTo({
			// 		url: '/pages/btdm/index/user/address/index',
			// 	})
			// },
			modify(flag) {
				this.focus = false
				this.numShow = true
				this.flag = flag
				if (flag == 1) {
					this.value = this.cusMoney.flhxMoney != '0.00' ? this.cusMoney.flhxMoney : ''
				} else if (flag == 2) {
					this.value = this.cusMoney.cxhxMoney != '0.00' ? this.cusMoney.cxhxMoney : ''
				}
				setTimeout(() => {
					this.focus = true
				}, 500);
			},
			addressData(address) {
				this.title = address.site;
				this.show = false
			},

			confirm() {
				if (Number(this.value) < 0) {
					this.$u.toast('输入余额不能小于0');
					return false;
				}
				// 返利
				if (this.flag == 1) {
					if (util.toFixed2(Number(this.value) + Number(this.cusMoney.cxhxMoney)) > Number(this.data.sumMoneyLmit)) {
						// this.$u.toast('返利金额促销金额之和不能超过上限');
						uni.showModal({
							title: '提示',
							content: '返利促销之和不能超过上限',
							showCancel: false,
							success: function(res) {}
						});
						return false;
					}

					if (Number(this.value) <= Number(this.data.flMoneyLmit)) {
						this.cusMoney.flhxMoney = util.toFixed2(this.value);
						this.payMoney = Number(this.data.sumMoney) - Number(this.cusMoney.flhxMoney) - Number(this.cusMoney
							.cxhxMoney) - Number(this.data.disMoney)

						this.payMoney = util.toFixed2(this.payMoney)
					} else {
						uni.showModal({
							title: '提示',
							content: '不能超过返利金额上限',
							showCancel: false,
							success: function(res) {}
						});
						// this.$u.toast('不能超过返利金额上限');
					}

					// if ((Number(this.value) + Number(this.cusMoney.cxhxMoney)) > Number(this.data.sumMoneyLmit)) {
					// 	this.$u.toast('返利金额促销金额之和不能大于商品总价');
					// 	return false;
					// }

					// // Number(this.data.cusMoney.appFlMoney)
					// if (Number(this.value) <= Number(this.data.flMoneyLmit)) {
					// 	this.cusMoney.flhxMoney = util.toFixed2(this.value);
					// 	this.payMoney = Number(this.data.sumMoney) - Number(this.cusMoney.flhxMoney) - Number(this.cusMoney
					// 		.cxhxMoney) - Number(this.data.disMoney)

					// 	this.payMoney = util.toFixed2(this.payMoney)
					// } else {
					// 	this.$u.toast('不能超过返利金额上限');
					// }
				}
				// 促销
				if (this.flag == 2) {
					if (util.toFixed2(Number(this.value) + Number(this.cusMoney.flhxMoney)) > Number(this.data.sumMoneyLmit)) {
						// this.$u.toast('返利金额促销金额之和不能超过上限');
						uni.showModal({
							title: '提示',
							content: '返利促销之和不能超过上限',
							showCancel: false,
							success: function(res) {}
						});
						return false;
					}

					if (Number(this.value) <= Number(this.data.cxMoneyLmit)) {
						this.cusMoney.cxhxMoney = util.toFixed2(this.value)
						this.payMoney = Number(this.data.sumMoney) - Number(this.cusMoney.flhxMoney) - Number(this.cusMoney
							.cxhxMoney) - Number(this.data.disMoney)
						this.payMoney = util.toFixed2(this.payMoney)
					} else {
						uni.showModal({
							title: '提示',
							content: '不能超过促销金额上限',
							showCancel: false,
							success: function(res) {}
						});
						// this.$u.toast('不能超过促销金额上限');
					}

					// if ((Number(this.value) + Number(this.cusMoney.flhxMoney)) > Number(this.data.sumMoney)) {
					// 	this.$u.toast('返利金额促销金额之和不能大于商品总价');
					// 	return false;
					// }
					// // Number(this.data.cusMoney.appCxMoney)
					// if (Number(this.value) <= Number(this.data.cxMoneyLmit)) {
					// 	this.cusMoney.cxhxMoney = util.toFixed2(this.value)
					// 	this.payMoney = Number(this.data.sumMoney) - Number(this.cusMoney.flhxMoney) - Number(this.cusMoney
					// 		.cxhxMoney) - Number(this.data.disMoney)
					// 	this.payMoney = util.toFixed2(this.payMoney)
					// } else {
					// 	this.$u.toast('不能超过促销金额上限');
					// }

				}

			}
		},


	}
</script>

<style lang="scss" scoped>
	page {
		width: 100%;
		height: 98%;
		text-align: center;
		overflow: scroll;
	}

	.item-menu-image {
		width: 100px;
		height: 85px;
	}

	textarea {
		width: 100%;
		height: 260rpx;
		background-color: #f7f7f7;
		line-height: 60rpx;
		// margin: 40rpx auto;
		padding: 20rpx;
	}
</style>